package com.egao.gzh.controller;


import com.egao.common.core.utils.FileServerUtil;
import com.egao.common.core.web.BaseController;
import com.egao.gzh.service.Wxservice;
import com.egao.gzh.utils.AesException;
import com.egao.gzh.utils.WXPublicUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * api登录相关接口
 * Created by lining on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * http://192.168.1.110/swagger-ui.html
 */
@RequestMapping("")
@RestController
@Api(value = "网页授权秘钥", tags = {"网页授权秘钥"})
public class WxNtController extends BaseController {

  @Value("${config.upload-location:0}")
  private Integer uploadLocation;  // 文件上传磁盘位置
  @Value("${config.open-office-home:}")
  private String openOfficeHome;  // OpenOffice的安装目录
  /**
   * 微信公众平台token
   * 服务器地址(URL)  本地用        http://5rayz9.natappfree.cc/api/nt/login/gzh
   * 服务器地址(URL)  正式服务器用   http://gzh.sszgjzx.com
   */
  @ApiOperation(value = "得到文件", notes = "" +
      "", httpMethod = "GET")
  @GetMapping("/MP_verify_2RNDdrbOIk7ai9hW.txt")
  public void gzhValidate(String signature, String timestamp,
                            String nonce, String echostr,
                          HttpServletResponse response, HttpServletRequest request){
//    System.out.println(System.getProperty("user.dir"));
//    String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//    System.out.println(path);
//
//    String path2 = ResourceUtils.getURL("classpath:").getPath();
//    System.out.println(path2);

    FileServerUtil.preview(new File(getUploadDir(),  "profile/MP_verify_2RNDdrbOIk7ai9hW.txt"), getPdfOutDir(), openOfficeHome, response, request);
    /* 文件上传目录位置 */


  }
  private String getUploadDir() {
    return getBaseDir() + "damonAppGzh/file/";
  }
  /* 文件存放位置 */
  private String getBaseDir() {
    return File.listRoots()[uploadLocation] + "damonAppGzh/upload/";
  }
  /* office预览生成pdf缓存位置 */
  private String getPdfOutDir() {
    return getBaseDir() + "pdf/";
  }
}
